package space.sakana.edusysback.mapper.job;

import org.apache.ibatis.annotations.*;
import space.sakana.edusysback.entity.job.Job;
import space.sakana.edusysback.entity.job.JobPush;
import space.sakana.edusysback.entity.job.JobPushItem;
import space.sakana.edusysback.entity.job.JobTarget;

import java.util.List;

@Mapper
public interface JobMapper {

    @Select("select `id`, `name`, `pid`, `tid`, `open`, `close` from job where `id`=#{id};")
    Job selectJobById(int id);

    @Select("select `id`, `name`, `pid`, `tid`, `open`, `close` from job;")
    List<Job> selectAllJob();

    @Insert("insert into job(`name`, `pid`, `tid`, `open`, `close`) values(#{name}, #{pid}, #{tid}, #{open}, #{close});")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    int insertJob(Job job);

    @Update("update job set `name`=#{name}, `tid`=#{tid}, `open`=#{open}, `close`=#{close} where `id`=#{id};")
    int updateJob(Job job);

    @Insert("insert into jobtarget(`uid`, `push`, `jid`) values(#{uid}, #{push}, #{jid});")
    int insertJobTarget(JobTarget jobTarget);

    @Update("update jobtarget set `jid`=#{jid}, `uid`=#{uid}, `push`=#{push} where `id`=#{id};")
    int updateJobTarget(JobTarget jobTarget);

    @Update("update jobtarget set `wait`=0 where `id`=#{id};")
    int finishJobTarget(JobTarget jobTarget);

    @Select("select `id`, `jid`, `uid`, `push` from jobtarget where `jid`=#{jid};")
    List<JobTarget> selectAllJobTarget(int jid);

    @Select("select `id`, `jid`, `uid`, `push` from jobtarget where `push`=1 and `wait`=0;")
    List<JobTarget> selectAllFinishJobTarget();

    @Select("select `id`, `jid`, `uid`, `push` from jobtarget where `id`=#{id};")
    JobTarget selectJobTarget(int id);

    @Select("select `id`, `jid`, `uid`, `push` from jobtarget where `jid`=#{jid} and `uid`=#{uid};")
    JobTarget selectJobTargetByUidAndJid(int uid, int jid);

    @Delete("delete from jobtarget where `uid`=#{uid}")
    int deleteJobTargetByUid(int uid);

    @Select("select `id`, `jid`, `uid`, `push` from jobtarget where `uid`=#{uid};")
    List<JobTarget> selectJobByUid(int uid);

    @Select("select `id`, `jid`, `uid`, `pid`, `qid`, `answer`, `score` from jobpushitem where `jid`=#{jid} and `uid`=#{uid} and `qid`=#{qid};")
    JobPushItem selectJobPushItem(int jid, int uid, int qid);

    @Insert("insert into jobpush(`jid`, `uid`, `pid`, `score`) values(#{jid}, #{uid}, #{pid}, #{score});")
    int insertJobPush(JobPush jobPush);

    @Delete("delete from jobpush where `uid`=#{uid}")
    int deleteJobPushByUid(int uid);

    @Update("update jobpush set `score`=#{score} where `id`=#{id};")
    int updateJobPushScore(JobPush jobPush);

    @Insert("insert into jobpushitem(`jid`, `uid`, `pid`, `qid`, `answer`, `score`) values(#{jid}, #{uid}, #{pid}, #{qid}, #{answer}, #{score});")
    int insetJobPushItem(JobPushItem jobPushItem);

    @Delete("delete from jobpushitem where `uid`=#{uid}")
    int deleteJobPushItemByUid(int uid);

    @Select("select `id`, `jid`, `uid`, `pid`, `score` from jobpush where `jid`=#{jid} and `uid`=#{uid}")
    JobPush selectJobPushByJidAndUid(int jid, int uid);

    @Update("update jobtarget set `wait`=0 where `id`=#{id};")
    int setJobUnWaited(int id);

    @Select("select `id`, `jid`, `uid`, `push` from jobtarget where `push`=1 and `wait`=1;")
    List<JobTarget> getAllWaitedJob();

    @Select("select `id`, `jid`, `uid`, `pid`, `qid`, `answer`, `score` from jobpushitem where `jid`=#{jid} and `uid`=#{uid};")
    List<JobPushItem> selectJobPushItemsByUidAndJid(int uid, int jid);

    @Update("update jobpushitem set `score`=#{score} where `id`=#{id};")
    int updateJobPushItemScore(JobPushItem jobPushItem);

    @Delete("delete from job where `id`=#{jid}")
    int deleteJobById(int jid);
}
